package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctDetectionItemsBo;
import com.rzdata.poct.system.domain.business.po.PoctDetectionItems;
import com.rzdata.poct.system.domain.business.vo.PoctDetectionItemsVo;
import com.rzdata.poct.system.mapper.PoctDetectionItemsMapper;
import com.rzdata.poct.system.service.IPoctDetectionItemsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * POCT检测项Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctDetectionItemsServiceImpl extends ServicePlusImpl<PoctDetectionItemsMapper, PoctDetectionItems, PoctDetectionItemsVo> implements IPoctDetectionItemsService {

    @Override
    public PoctDetectionItemsVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctDetectionItemsVo> queryPageList(PoctDetectionItemsBo bo) {
        PagePlus<PoctDetectionItems, PoctDetectionItemsVo> result =
            pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctDetectionItemsVo> queryList(PoctDetectionItemsBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctDetectionItems> buildQueryWrapper(PoctDetectionItemsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctDetectionItems> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDetectionId() != null, PoctDetectionItems::getDetectionId, bo.getDetectionId());
        lqw.eq(bo.getItemId() != null, PoctDetectionItems::getItemId, bo.getItemId());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), PoctDetectionItems::getItemName, bo.getItemName());
        lqw.like(StringUtils.isNotBlank(bo.getItemEname()), PoctDetectionItems::getItemEname, bo.getItemEname());
        lqw.eq(StringUtils.isNotBlank(bo.getDetectionValue()), PoctDetectionItems::getDetectionValue, bo.getDetectionValue());
        lqw.eq(StringUtils.isNotBlank(bo.getReferenceValue()), PoctDetectionItems::getReferenceValue, bo.getReferenceValue());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), PoctDetectionItems::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getDetectionMethod()), PoctDetectionItems::getDetectionMethod, bo.getDetectionMethod());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctDetectionItemsBo bo) {
        PoctDetectionItems add = BeanUtil.toBean(bo, PoctDetectionItems.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctDetectionItemsBo bo) {
        PoctDetectionItems update = BeanUtil.toBean(bo, PoctDetectionItems.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctDetectionItems entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
